<!--
 * @Author       : wfl
 * @LastEditors: fj
 * @description  :
 * @updateInfo   :
 * @Date         : 2023-07-26 12:16:21
 * @LastEditTime: 2023-08-10 18:28:51
-->
<script lang="ts" setup name="NewsTemplateConfig">
import { useNews } from './useNews'

const emits = defineEmits<{
  (e: 'next'): void
}>()
const SystemChannel = defineAsyncComponent(() => import('./components/SystemChannel.vue'))
const WxChannel = defineAsyncComponent(() => import('./components/WxChannel.vue'))
const DdChannel = defineAsyncComponent(() => import('./components/DdChannel.vue'))
const SmsChannel = defineAsyncComponent(() => import('./components/SmsChannel.vue'))

const {
  ESendType, getMsgChannelByNo,
  newsStore,
  msgBoxWarnAsync,
  tempForm
} = useNews()

provide('tempForm', tempForm)

const activeName = ref('1')
const channelData: Ref<any[]> = ref([]);
(async () => {
  channelData.value = await getMsgChannelByNo()
})()

const SYSTEM_CHANNEL = computed(() => channelData.value.find((item: any) => item.channel === ESendType.系统消息))
const WX_CHANNEL = computed(() => channelData.value.find((item: any) => item.channel === ESendType.微信小程序))
const SMS_CHANNEL = computed(() => channelData.value.find((item: any) => item.channel === ESendType.短信))
const DD_CHANNEL = computed(() => channelData.value.find((item: any) => item.channel === ESendType.钉钉群机器人))

const complete = () => {
  const condifIndex = channelData.value.map((it: any) => it.configured).findIndex(it => it)
  const stateIndex = channelData.value.map((it: any) => it.status).findIndex(it => it === 'ENABLED')
  // 未配置提示
  if (condifIndex < 0) {
    msgBoxWarnAsync('未完成确认', '您还未完成系统消息配置，请选择启用、保存不启用操作后，再点击完成按钮。', async () => {

    })
    return
  }
  // 未启用
  if (stateIndex < 0) {
    msgBoxWarnAsync('完成确认', '您还未启用任意一个消息推送渠道，系统无法及时将消息推送给用户。确定完成吗？', async () => {
      emits('next')
    })
    newsStore.setIsEnabled(false)
    return
  }
  else {
    newsStore.setIsEnabled(true)
  }
  emits('next')
}
const closeCollapse = () => {
  activeName.value = ''
}

const disabledChannel = ref(new Map())

const handSetDisabled = (channel: any) => {
  activeName.value = ''
  disabledChannel.value.set(channel, true)
}
</script>

<template>
  <el-collapse v-model="activeName" accordion>
    <el-collapse-item title="系统消息" name="1" :disabled="disabledChannel.has(ESendType.系统消息)">
      <SystemChannel
        :data="SYSTEM_CHANNEL"
        flag="sys"
        @success="handSetDisabled(ESendType.系统消息)"
        @close="closeCollapse"
      />
    </el-collapse-item>
    <el-collapse-item title="微信小程序-订阅消息" name="2" :disabled="disabledChannel.has(ESendType.微信小程序)">
      <WxChannel
        :data="WX_CHANNEL"
        @close="closeCollapse"
        @success="handSetDisabled(ESendType.微信小程序)"
      />
    </el-collapse-item>
    <el-collapse-item title="短信" name="3" :disabled="disabledChannel.has(ESendType.短信)">
      <SmsChannel
        :data="SMS_CHANNEL"
        @close="closeCollapse"
        @success="handSetDisabled(ESendType.短信)"
      />
    </el-collapse-item>
    <el-collapse-item title="钉钉群机器人消息" disabled name="4">
      <DdChannel :data="DD_CHANNEL" @close="closeCollapse" />
    </el-collapse-item>
  </el-collapse>
  <el-button
    type="primary"
    style="margin-top:20px"
    @click="complete"
  >
    完成
  </el-button>
</template>
